package com.zmzncs.lmtc.module.shop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.shop.pojo.shop_goods.*;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.*;
import com.zmzncs.lmtc.module.shop.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.executable.ValidateOnExecution;
import java.util.List;

/**
 * <p>
 * 商户-商品 前端控制器
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Api(tags = "商品信息")
@RestController
@RequestMapping("/shop/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @ApiOperation(value = "添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("add")
    public Rest add(@Valid GoodsAddForm form){
        form.validate();
        goodsService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "条件分页查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("getGoodsPage")
    public Rest<PageInfo<GoodsTableVO>> getGoodsPage(@Valid GoodsSelectForm form){
        IPage<GoodsDTO> goodsDTOIPage = goodsService.getGoodsPage(form);
        PageInfo<GoodsTableVO> pageInfo = JsonUtil.pageTransform(goodsDTOIPage, GoodsTableVO.class);
        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "商品管理—删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "goodsId", value = "商品ID", required = true),
    })
    @PostMapping("delete")
    public Rest delete(@NotNull Long goodsId){

        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setIsDeleted(1);
        goodsService.updateById(goods);

        return RestUtil.success();
    }

    @ApiOperation(value = "商品管理—修改展示状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "goodsId", value = "商品ID", required = true),
            @ApiImplicitParam(paramType="query", name = "status", value = "状态（1：展示，0：不展示）", required = true),
    })
    @PostMapping("updateStatus")
    public Rest updateStatus(@NotNull Long goodsId, @NotNull Integer status){

        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setStatus(status);
        goodsService.updateById(goods);

        return RestUtil.success();
    }

    @ApiOperation(value = "根据id查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "goodsId", value = "商品ID", required = true),
    })
    @GetMapping("getGoodsById")
    public Rest<GoodsVO> getGoodsById(@NotNull Long goodsId){

        GoodsDTO goodsDTO = goodsService.getGoodsById(goodsId);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goodsDTO,goodsVO);

        return RestUtil.success(goodsVO);
    }

    @ApiOperation(value = "修改商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("update")
    public Rest updateGoods(@Valid GoodsUpdateForm form){
        form.validate();
        goodsService.updateGoods(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "根据id查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "shopId", value = "商户Id", required = true),
    })
    @GetMapping("getGoodsByShopId")
    public Rest<List<GoodsDTO>> getGoodsByShopId(@NotNull Long shopId){
        List<GoodsDTO> goodsDTOS = goodsService.getGoodsByShopId(shopId);

        return RestUtil.success(goodsDTOS);
    }

}

